<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTool :data="company">
          <el-dropdown-item>添加子部门</el-dropdown-item>
        </TreeTool>
        <el-tree v-loading="isLoading" :data="list" :props="{label: 'name'}" default-expand-all>
          <template v-slot="{data}">
            <tree-tool :data="data">
              <el-dropdown-item @click.native="onAdd(data)">添加子部门</el-dropdown-item>
              <el-dropdown-item @click.native="onEdit(data)">编辑部门</el-dropdown-item>
              <el-dropdown-item @click.native="onDel(data.id)">删除部门</el-dropdown-item>
            </tree-tool>
          </template>
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="addDeptRef" :visible.sync="showDialog" :node="currentNode" @success="getList()"></add-dept>
  </div>
</template>

<script>
import TreeTool from './components/tree-tool.vue'
import { delDept, getDeptById, getList } from '@/api/departments.js'
import { translateListToTree } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    TreeTool,
    AddDept
  },
  props: {},
  data() {
    return {
      isLoading: false,
      showDialog: false, // 控制对话框显示
      currentNode: {}, // 当前操作的部门
      company: { name: '', manager: '负责人' },
      list: []
    }
  },
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    async getList() {
      this.isLoading = true
      const { companyName, depts } = await getList()
      this.company.name = companyName
      this.list = translateListToTree(depts, '')
      this.isLoading = false
    },
    onAdd(node) {
      this.currentNode = node
      this.showDialog = true
    },
    async onEdit(node) {
      console.log('编辑')
      this.currentNode = node
      this.$refs.addDeptRef.form = await getDeptById(node.id)
      this.showDialog = true
    },
    onDel(id) {
      console.log('删除', id)
      // 确认删除
      // 通过elmecon
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        // 发请求
        await delDept(id)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新
        this.getList()
      }).catch(() => {

      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
.el-dropdown-link{
  margin-left: 10px;
}
</style>
